//
//  CZMessageCell.m
//  Final
//
//  Created by lanou3g on 15/12/5.
//  Copyright © 2015年 皮比诺. All rights reserved.
//

#import "CZMessageCell.h"
#import "CZMessage.h"
#import "CZMessageFrame.h"
#import "UIImage+Extension.h"

@interface CZMessageCell ()
@property(nonatomic, strong)UILabel *timeView;
@property(nonatomic, strong)UIImageView *iconView;
@property(nonatomic, strong)UIButton *textView;

@end


@implementation CZMessageCell

//创建zi定义cell的对象
+(instancetype)messageCellWithTableview:(UITableView *)tableView{
    
    static NSString *reuseId = @"msg";
    CZMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    
    if (cell == nil) {
        cell = [[CZMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
        
    
    
    }
    
    return  cell;
}

//创建子控件
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        
        self.backgroundColor = [UIColor clearColor];
        //时间
        UILabel *timeView = [[UILabel alloc]init];
        [self.contentView addSubview:timeView];
        self.timeView = timeView;
        timeView.textAlignment = NSTextAlignmentCenter;
        
        
        
        
        
        //头像
        UIImageView *iconView = [[UIImageView alloc]init];
        [self.contentView addSubview:iconView];
        self.iconView = iconView;
        //设置圆形
        iconView.layer.cornerRadius = 25;
        iconView.layer.masksToBounds = YES;
        
        
        
        //聊天内容
        UIButton *textView = [[UIButton alloc]init];
        [self.contentView addSubview:textView];
        self.textView = textView;
        //设置字体大小
        textView.titleLabel.font = [UIFont systemFontOfSize:CZFontSize];
        //设置换行
        textView.titleLabel.numberOfLines = 0;
        //颜色
        [textView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //设置按钮内容的边距
        textView.contentEdgeInsets = UIEdgeInsetsMake(20, 20, 20, 20);
        
     }
    
     return self;
   
}



- (void)setMessageFrame:(CZMessageFrame *)messageFrame
{
    
    _messageFrame = messageFrame;
    
    CZMessage *msg = messageFrame.message;
    
    
    self.timeView.text = msg.time;
    
    NSString *imgName = msg.type == CZMessageTypeSelf ? @"me" : @"other";
    
    self.iconView.image = [UIImage imageNamed:imgName];
    [self.textView setTitle:msg.text forState:UIControlStateNormal];
    
    
    if (msg.type == CZMessageTypeSelf) {
        
        [self.textView setBackgroundImage:[UIImage resizeImage:@"chat_send_nor"] forState:UIControlStateNormal];
        [self.textView setBackgroundImage:[UIImage resizeImage:@"chat_send_press_pic"] forState:UIControlStateHighlighted];
        
    }else{
        
        [self.textView setBackgroundImage:[UIImage resizeImage:@"chat_recive_nor"] forState:UIControlStateNormal];
        [self.textView setBackgroundImage:[UIImage resizeImage:@"chat_recive_press_pic"] forState:UIControlStateHighlighted];
 
    }
    
  //设置子控件的frame
    
    
    self.iconView.frame = messageFrame.iconFrame;
    self.textView.frame = messageFrame.textFrame;
    self.timeView.frame = messageFrame.timeFrame;
    
    
    
}











- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
